import _ from 'lodash';
import { types } from ".";

export default class DirRefs {
  name = 'refs';
  type = types.FUNCTION;

  constructor(refs) {
    this.refs = refs;
  }

  with(value) {
    let refs = this.refs;

    if (_.isFunction(refs)) {
      const values = refs(value);

      if (_.isArray(values)) return values;

      return Object.keys(values).map(key => {
        return { key, value: values[key] };
      });
    }

    if (_.isPlainObject(refs)) {
      refs = [refs];
    }

    return refs.map(ref => {
      const key = Object.keys(ref)[0];
      let handle = ref[key];

      if (_.isString(handle)) {
        handle = () => handle;
      }

      return { key, value: handle(value) };
    });
  }
}